package com.coin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coin.domain.*;
import com.coin.model.R;
import com.coin.service.CashWithdrawalsService;
import com.coin.vo.CashSellParam;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @Auther: 李 力
 * @Date: 2025/6/13
 * @Description: com.coin.controller
 * @version: 1.0
 */
@RestController
@RequestMapping("/cashWithdrawals")
public class CashWithdrawalsController {

    @Autowired
    private CashWithdrawalsService cashWithdrawalsService;

    /**
     * @Description: 条件分页查询
     * @Author: 李 力
     * @Date: 2025/6/13
     * @Param:
     * @return: R
     */
    @GetMapping("/records")
    @ApiOperation(value = "条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数"),
            @ApiImplicitParam(name = "coinId", value = "币种的Id"),
            @ApiImplicitParam(name = "userId", value = "用户的Id"),
            @ApiImplicitParam(name = "mobile", value = "用户的手机号"),
            @ApiImplicitParam(name = "status", value = "充值审核状态"),
            @ApiImplicitParam(name = "numMin", value = "充值最小金额"),
            @ApiImplicitParam(name = "numMax", value = "充值最大金额"),
            @ApiImplicitParam(name = "startTime", value = "充值开始时间"),
            @ApiImplicitParam(name = "endTime", value = "充值结束时间"),
    })
    public R<Page<CashWithdrawals>> findByPage(
            @ApiIgnore Page<CashWithdrawals> page,
            Long coinId, Long userId, String userName,
            String mobile, Byte status, String numMin, String numMax,
            String startTime, String endTime
    ) {
        Page<CashWithdrawals> pageData = cashWithdrawalsService.findByPage(page, coinId, userId, userName, mobile, status, numMin, numMax, startTime, endTime);
        return R.ok(pageData);
    }


    /**
     * @Description: GCN的卖出操作
     * @Author: 李 力
     * @Date: 2025/6/19
     * @Param:
     * @return:
     */
    @PostMapping("/sell")
    @ApiOperation(value = "GCN的卖出操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "", value = "")
    })
    public R<Object> sell(@RequestBody CashSellParam cashSellParam) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        boolean isOk = cashWithdrawalsService.sell(userId, cashSellParam);
        return isOk ? R.ok() : R.fail("操作失败");
    }

    /**
     * @Description: 审核操作
     * @Author: 李 力
     * @Date: 2025/6/19
     * @Param:
     * @return: R
     */
    @PostMapping("/updateWithdrawalsStatus")
    public R updateWithdrawalsStatus(@RequestBody CashWithdrawAuditRecord record) {
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        boolean isOk = cashWithdrawalsService.updateWithdrawalsStatus(Long.valueOf(userId), record);
        return isOk ? R.ok() : R.fail("审核失败");
    }

    /**
     * @Description: 查询当前用户的提现记录
     * @Author: 李 力
     * @Date: 2025/6/19
     * @Param:
     * @return: R
     */
    @GetMapping("/user/records")
    @ApiOperation(value = "查询当前用户的提现记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的大小"),
            @ApiImplicitParam(name = "status", value = "充值的状态"),
    })
    public R<Page<CashWithdrawals>> findUserCashRecharge(@ApiIgnore Page<CashWithdrawals> page, Byte status) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        Page<CashWithdrawals> cashRechargePage = cashWithdrawalsService.findUserCashRecharge(page, userId, status);
        return R.ok(cashRechargePage);
    }
}
